package com.k8sops.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.k8sops.backend.common.ApiResponse;
import com.k8sops.backend.model.entity.DOSProject;
import com.k8sops.backend.service.DOSProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Controller
 */
@RestController
@RequestMapping("/project")
public class DOSProjectController {

    @Autowired
    private DOSProjectService projectService;

    /**
     * Add a new project
     */
    @PostMapping("/add")
    public ApiResponse<Boolean> addProject(@RequestBody DOSProject project) {
        boolean result = projectService.addProject(project);
        return result ? ApiResponse.success(true) : ApiResponse.error("添加项目失败");
    }

    /**
     * Update project information
     */
    @PutMapping("/update")
    public ApiResponse<Boolean> updateProject(@RequestBody DOSProject project) {
        if (project.getId() == null) {
            return ApiResponse.error("项目ID不能为空");
        }
        boolean result = projectService.updateProject(project);
        return result ? ApiResponse.success(true) : ApiResponse.error("更新项目失败");
    }

    /**
     * Delete a project by ID
     */
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Boolean> deleteProject(@PathVariable Long id) {
        boolean result = projectService.deleteProjectById(id);
        return result ? ApiResponse.success(true) : ApiResponse.error("删除项目失败");
    }

    /**
     * Get project by ID
     */
    @GetMapping("/get/{id}")
    public ApiResponse<DOSProject> getProject(@PathVariable Long id) {
        DOSProject project = projectService.getProjectById(id);
        return project != null ? ApiResponse.success(project) : ApiResponse.error("项目不存在");
    }

    /**
     * Page query projects
     */
    @GetMapping("/page")
    public ApiResponse<IPage<DOSProject>> pageProjects(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String projectName,
            @RequestParam(required = false) Integer groupId) {
        IPage<DOSProject> result = projectService.pageProjects(page, size, projectName, groupId);
        return ApiResponse.success(result);
    }
}
